package internet_11;
/*
    服务器：接收到的数据写入文本文件，给出反馈，代码用线程进行封装，为每一个客户端开启一个线程
 */
import java.io.*;
import java.net.Socket;

public class SerThread implements Runnable {
    private Socket s;
    public SerThread(Socket s) {
        this. s=s;
    }

    @Override
    public void run() {
        try {
            //读写数据
            BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));//读取数据

            //解决重名问题
            int count=1;
            File file=new File("day12_internet\\ss("+count+").txt");
            while (file.exists()) {
                count++;
                file = new File("day12_internet\\ss(" + count + ").txt");
            }


            BufferedWriter bw=new BufferedWriter(new FileWriter(file));//写入文本
            String line;
            while ((line=br.readLine())!=null){
                bw.write(line);
                bw.newLine();
                bw.flush();
            }

            //给出反馈
            BufferedWriter bww=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            bww.write("文件上传成功");
            bww.newLine();
            bww.flush();



            //异常处理
            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
